/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdQuests.QuestMaker;

import com.SySammy.sdQuests.Handlers.yamlHandler;
import com.SySammy.sdQuests.Handlers.InheritanceHandler;
import com.SySammy.SdCurrency.sdCCommandExport;
import com.SySammy.sdRankingSys.sdRS;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class QuestMethods {

    private InheritanceHandler IH = new InheritanceHandler();
    private int Numb;
    public static HashMap<Player, ArrayList<String>> RunningQuests = new HashMap<Player, ArrayList<String>>();

    public QuestMethods() {

        this.Numb = YH.NumberOfQuests();
    }
    private yamlHandler YH = new yamlHandler();

    public void QuestList(Player p) {
        p.sendMessage(ChatColor.DARK_AQUA + "Press /quest -i [name]" + ChatColor.GOLD + " for more info about a quest.");
        p.sendMessage(ChatColor.DARK_AQUA + "Press /quest -s [name]" + ChatColor.GOLD + " for starting a quest.");
        for (int x = 0; x < Numb; x++) {
            if (YH.getQuestName(x) != null) {
                if (IH.loadQuestPart(p.getName(), YH.getQuestName(x)) > YH.NumberOfParts(YH.getQuestName(x))) {
                    p.sendMessage(ChatColor.GREEN + YH.getQuestName(x) + ": " + ChatColor.GOLD + "Quest Completed.");
                    continue;
                }
                p.sendMessage(ChatColor.DARK_AQUA + "Name: " + ChatColor.GOLD + YH.getQuestName(x));
                p.sendMessage(ChatColor.DARK_AQUA + "Parts: " + ChatColor.GOLD + YH.NumberOfParts(YH.getQuestName(x)));
                p.sendMessage("");
            }
        }
    }

    public void QuestInfo(Player p, String quest) {
        String part = String.valueOf(IH.loadQuestPart(p.getName(), quest));
        if (IH.loadQuestPart(p.getName(), quest) > YH.NumberOfParts(quest)) {
            p.sendMessage(ChatColor.RED + "You already ended that quest.");
            return;
        }
        p.sendMessage(ChatColor.DARK_AQUA + "Part: " + ChatColor.GREEN + YH.getPartCount(quest, part));
        p.sendMessage(ChatColor.DARK_AQUA + "Name: " + ChatColor.GOLD + YH.getPartName(quest, part));
        p.sendMessage(ChatColor.DARK_AQUA + "Info: " + ChatColor.GOLD + YH.getInfo(quest, part));
        p.sendMessage(ChatColor.DARK_AQUA + "Reward: " + ChatColor.GOLD + YH.getRewardInfo(quest, part));
    }

    public boolean QuestExists(Player p, String Quest) {
        return YH.QuestExists(Quest);
    }

    public void AcceptQuest(Player p, String quest) {
        String part = "part" + String.valueOf(IH.loadQuestPart(p.getName(), quest));
        ArrayList<String> quests = new ArrayList<String>();
        quests.add(quest + ":" + part);
        if (IH.loadQuestPart(p.getName(), quest) > YH.NumberOfParts(quest)) {
            p.sendMessage(ChatColor.RED + "Sorry, you're already done all parts of that quest.");
            return;
        }
        if (RunningQuests.size() <= 0 || !RunningQuests.get(p).contains(quests.get(0))) {
            RunningQuests.put(p, quests);
            p.sendMessage(ChatColor.GREEN + "Quest started, good luck on your endeavour.");
        } else {
            p.sendMessage(ChatColor.RED + "Sorry, you're already doing that quest");
        }
    }

    public void ReturnQuest(Player p, String quest) {
        String part = "part" + String.valueOf(IH.loadQuestPart(p.getName(), quest));
        ArrayList<String> quests = new ArrayList<String>();
        quests.add(quest + ":" + part);
        if (RunningQuests.size() <= 0) {
            p.sendMessage(ChatColor.RED + "Sorry, you aren't doing that quest");
            return;
        }
        if (RunningQuests.size() >= 0 && RunningQuests.get(p).contains(quests.get(0))) {
            InventoryCheck(p, quest);
            IH.saveQuestPart(p.getName(), quest);
            if (IH.loadQuestPart(p.getName(), quest) <= YH.NumberOfParts(quest)) {
                p.sendMessage(ChatColor.DARK_AQUA + "Quest part done, the next part is now starting.");
                AcceptQuest(p, quest);
            } else {
                p.sendMessage(ChatColor.GREEN + "Congrats, quest finished with glory.");
            }
        } else {
            p.sendMessage(ChatColor.RED + "Sorry, you aren't doing that quest");
        }
    }

    private void InventoryCheck(Player p, String quest) {
        String part = String.valueOf(IH.loadQuestPart(p.getName(), quest));
        Inventory plyInv = p.getInventory();
        ItemStack prize = new ItemStack(Material.getMaterial(YH.getAskedID(quest, part)), YH.getAskedQnt(quest, part));
        HashMap<Integer, ItemStack> removeItem = p.getInventory().removeItem(new ItemStack(YH.getAskedID(quest, part), YH.getAskedQnt(quest, part)));
        if (removeItem.isEmpty()) {
            sdCCommandExport CE = new sdCCommandExport();
            CE.GiveMoney(p.getName(), YH.getRewardMoney(quest, part));
            plyInv.addItem(new ItemStack(YH.getRewardID(quest, part), YH.getRewardQnt(quest, part)));
            RunningQuests.get(p).remove(quest + ":" + part);
            sdRS.AddExp(p, YH.getRewardExp(quest, part));
        } else {
            prize.setAmount(prize.getAmount() - removeItem.get(0).getAmount());
            if (prize.getAmount() == 0) {
                p.sendMessage(ChatColor.RED + "You don't have the required item Stack.");
                return;
            }
            p.getInventory().addItem(prize);
            p.sendMessage(ChatColor.RED + "You don't have the required item Stack.");
        }
    }
}
